home *** CD-ROM | disk | FTP | other *** search
- /*
- * The routines in this file read and write ASCII files from the disk. All of
- * the knowledge about files are here. A better message writing scheme should
- * be used.
- */
-
- #include "ed.h"
- #include "osbind.h"
-
- #define SZtextbuf 512
- static int fd; /* File descriptor, all functions. */
- static int rwflag; /* 0=read or 1=write flag */
- static char textbuf[SZtextbuf];
- static int x = 0;
- static int n = 0;
- static int sz = SZtextbuf;
- static char *txtp;
- static int lnn; /* line number */
-
- static /* allocate large buffer for */
- fbfalloc() /* file io */
- {
- txtp = (char *) malloc(sz = 16*1024);
- if (txtp == NULL) {
- txtp = textbuf;
- sz = SZtextbuf;
- }
- rwflag = x = n = 0;
- }
-
- static
- fbffree()
- {
- if (txtp != textbuf) free(txtp);
- rwflag = x = n = 0; /* not nec */
- sz = SZtextbuf; /* not nec */
- }
-
- /*
- * Open a file for reading.
- */
- ffropen(fn)
- char *fn;
- {
- if ((fd=Fopen(fn, 0)) < 0) return (FIOFNF);
- fbfalloc();
- lnn = 0;
- return (FIOSUC);
- }
-
- /*
- * Open a file for writing. Return TRUE if all is well, and FALSE on error
- * (cannot create).
- */
- ffwopen(fn)
- char *fn;
- {
- char lfn[NFILEN];
-
- strcpy(lfn, fn); /* ST BIOS uppercases filename! */
- if ((fd=Fcreate(lfn, 0)) < 0) {
- mlwrite("Cannot open file for writing");
- return (FIOERR);
- }
- fbfalloc();
- rwflag = 1;
- return (FIOSUC);
- }
-
- /*
- * Close a file. Should look at the status in all systems.
- */
- ffclose()
- {
- if ((rwflag == 1) && (x < sz)) {
- if (Fwrite(fd, (long) x, txtp) < 0) {
- mlwrite("Write I/O error");
- return (FIOERR);
- }
- }
- Fclose(fd); /* check for errors ?? */
- fbffree();
- return (FIOSUC);
- }
-
- /*
- * Write a line to the already opened file. The "buf" points to the buffer,
- * and the "nbuf" is its length, less the free newline. Return the status.
- * Check only at the newline.
- */
- ffputline(buf, nbuf)
- char buf[];
- {
- register int i;
- register char *p;
- static char crlf[] = "\r\n";
-
- p = buf;
- xxxx:
- for (i = 0; i < nbuf; ++i) {
- txtp[x++] = *p++;
- if (x == sz) {
- if (Fwrite(fd, (long) x, txtp) < 0) {
- mlwrite("Write I/O error");
- return (FIOERR);
- }
- x = 0;
- }
- }
- if (p != &crlf[2]) {
- p = crlf;
- nbuf = 2;
- goto xxxx;
- }
- return (FIOSUC);
- }
-
- /*
- * Read a line from a file, and store the bytes in the supplied buffer. The
- * "nbuf" is the length of the buffer. Complain about long lines and lines
- * at the end of the file that don't have a newline present. Check for I/O
- * errors too. Return status.
- */
- ffgetline(buf, nbuf, nb)
- register char buf[];
- int nbuf;
- int *nb;
- {
- register int i;
- register char c;
-
- i = 0;
- for (;;) {
- if (x == n) {
- n = Fread(fd, (long) sz, txtp);
- x = 0;
- }
- if (x >= n) break;
- c = txtp[x++];
- if (c == '\n') {
- lnn ++;
- if (i > 0 && buf[i-1] == '\r') i--;
- break;
- }
- buf[i++] = c;
- if (i == nbuf) {
- mlwrite("long line %d is being split...", lnn);
- break;
- }
- }
- buf[i] = '\000';
- *nb = i;
-
- if (x > n) {
- mlwrite("File read error");
- return (FIOERR);
- }
-
- if (n == 0) {
- if (i != 0) {
- mlwrite("The last line ended without \\n");
- return (FIOERR);
- }
- return (FIOEOF);
- }
- return (FIOSUC);
- }
-
- /* -eof- */
-